<html>
<head>
    <link href="${ctx}/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet">
    <link href="${ctx}/font-awesome/3.0.0/css/font-awesome.min.css" rel="stylesheet">
    <link href="${ctx}/css/index.css" rel="stylesheet">
    <link href="${ctx}/font-awesome/3.0.0/css/font-awesome.min.css" rel="stylesheet">
    <link href="${ctx}/bootstrap-table/1.12.0/bootstrap-table.min.css" rel="stylesheet">
    <script src="${ctx}/jquery/2.1.4/jquery.min.js"></script>
    <script src="${ctx}/bootstrap/3.3.7/js/bootstrap.min.js"></script>
    <script src="${ctx}/bootstrap-table/1.12.0/bootstrap-table.min.js"></script>
</head>
<body>
<div class="container-fluid">
    <div id="toolbar">
        <div class="input-group" style="float: left; width: 200px; margin:0 15px 10px 0;">
            <span class="input-group-addon">应用名称</span>
            <input type="text" class="form-control" id="appid">
        </div>
        <div class="input-group" style="float: left; width: 200px; margin:0 15px 10px 0;">
            <span class="input-group-addon">应用商家</span>
            <input type="text" class="form-control" id="appStore" value="">
        </div>
        <div class="input-group" style="float: left; width: 200px;margin:0 15px 10px 0;">
            <span class="input-group-addon">应用版本</span>
            <input type="text" class="form-control" id="innerCode">
        </div>
        <div class="input-group" style="float: left; max-width: 380px;margin-bottom: 10px">
            <span class="input-group-addon">创建时间</span>
            <input type="date" class="form-control" style="position:relative; float:left; width:140px" id="startTime">
            <input type="date" class="form-control" style="position:relative; float:left; width:140px" id="endTime">
        </div>
        <div class="btn-group" style="margin-right: 10px">
            <button id="search_btn" class="btn btn-info">立即查询</button>
            <button id="insert_btn" class="btn btn-success" style="margin-left: 15px">版本发布</button>
            <button onclick="delete_cache()" class="btn btn-success" style="margin-left: 15px">清除缓存</button>
        </div>


    </div>
    <table id="table"></table>
</div>
<script src="${ctx}/bootstrap-table/1.12.0/locale/bootstrap-table-zh-CN.min.js"></script>
<script src="${ctx}/bootstrap-table/1.12.0/extensions/export/bootstrap-table-export.min.js"></script>
<script src="${ctx}/js/extends.js"></script>
<script>

    function queryParams(opt) {
        opt.appid = $("#appid").val();
        opt.createTime = $("#startTime").val();
        opt.appStore = $("#appStore").val();
        opt.innerCode = $("#innerCode").val();
        opt.version = $("#version").val();
        opt.packagename = $("#packagename").val();
        opt.forceUpdate = $("#forceUpdate").val();
        opt.modifyTime = $("#endTime").val();
        return opt;
    };

    function delete_cache() {
        $.ajax({
            url: "/app/deleteCache",    //请求的url地址
            dataType: "json",   //返回格式为json
            async: true,//请求是否异步，默认为异步，这也是ajax重要特性
            data: {status: 1},    //参数值,键值对
            type: "post",   //请求方式
            beforeSend: function () {
                //请求前的处理
            },
            success: function (req) {
                //请求成功时处理
                alert(req.message)
            },
            complete: function () {
                //请求完成的处理
            },
            error: function () {
                //请求出错处理
                layer.msg("请重试！若在失败请联系管理员")
            }
        });
    }

    var table = null;

    $(function () {
        table = $('#table')
        //加载表格
        table.bootstrapTable({
            url: '/apps/version', //请求后台的URL（*）
            method: 'post', //请求方式（*）
            toolbar: '#toolbar', //工具按钮用哪个容器
            striped: true,
            dataType: "json",
            pagination: true,
            // "queryParamsType": "limit",
            singleSelect: false,
            contentType: "application/x-www-form-urlencoded",
            pageSize: 50,
            pageNumber: 1,
            search: false, //不显示 搜索框
            showColumns: false, //不显示下拉框（选择显示的列）
            sidePagination: "server", //服务端请求
            queryParams: queryParams,
            //minimunCountColumns: 2,
            pageList: [50, 100, 200, 500], //可供选择的每页的行数（*）
            //queryParams : oTableInit.queryParams,//传递参数（*）
            searchOnEnterKey: true,
            // search: true, //是否显示表格搜索，此搜索是客户端搜索，不会进服务端，所以，个人感觉意义不大
            strictSearch: true,
            //showColumns: true, //是否显示所有的列
            showRefresh: false, //是否显示刷新按钮
            minimumCountColumns: 2, //最少允许的列数
            clickToSelect: false, //是否启用点击选中行
            height: (document.body.clientHeight), //行高，如果没有设置height属性，表格自动根据记录条数觉得表格高度
            uniqueId: "code", //每一行的唯一标识，一般为主键列
            // showToggle: true, //是否显示详细视图和列表视图的切换按钮
            // cardView: false, //是否显示详细视图
            // detailView: false, //是否显示父子表
            // showExport: true, //是否显示导出
            exportDataType: "selected", //basic', 'all', 'selected'
            columns: [{
                checkbox: true
            }, {
                title: 'ID',
                width: '60px',
                formatter: function (value, row, index) {
                    //   console.log(row)
                    return index + 1;
                }
            }, {
                field: 'createTime',
                title: '创建时间',
                width: '125px',
                formatter: function (value, row, index) {
                    return new Date(value).format("yyyy-MM-dd HH:mm:ss");
                }
            }, {
                field: 'appId',
                title: '应用名称'
            }, {
                field: 'appStore',
                title: '应用商家',
            }, {
                field: 'href',
                title: '下载地址'
            }, {
                field: 'innerCode',
                title: '内置版本'
            }, {
                field: 'title',
                title: '版本标题'
            }, {
                field: 'content',
                title: '版本内容'
            }, {
                field: 'appMode',
                title: '状态',
                formatter: function (value, row, index) {
                    if (value == 'Beta') {
                        return "已上架";
                    } else {
                        return "开发中";
                    }
                    return btns;
                }
            }, {
                field: 'uuid',
                title: '操作',
                width: '280px',
                formatter: function (value, row, index) {
                    if (row.tipUser == 'False' && row.forceUpdate == 'False') {
                        // 	console.log(row);
                        var btns = '<a  onClick="alertUpdate(\'' + row.appStore + '\',\'' + row.innerCode + '\')">未提示更新</a>&nbsp;&nbsp;&nbsp;&nbsp;'
                            + '<a onClick="forceUpdate(\'' + row.appStore + '\',\'' + row.innerCode + '\',\'' + row.forceUpdate + '\')">未强制更新</a>&nbsp;&nbsp;&nbsp;&nbsp;'
                            + '<a href="javascript:void(0);" onClick="edit(\'' + row.appStore + '\',\'' + row.innerCode + '\')">编辑</a>&nbsp;&nbsp;&nbsp;&nbsp;'
                            + '<a href="javascript:void(0);" onClick="mode(\'' + row.appStore + '\',\'' + row.innerCode + '\')">上下架</a>&nbsp;&nbsp;&nbsp;&nbsp;'
                            + '<a href="javascript:void(0);" onClick="del(\'' + row.appStore + '\',\'' + row.innerCode + '\')">删除</a>';
                    } else if (row.tipUser = 'False' && row.forceUpdate == 'True') {
                        var btns = '<a  onClick="alertUpdate(\'' + row.appStore + '\',\'' + row.innerCode + '\')">未提示更新</a>&nbsp;&nbsp;&nbsp;&nbsp;'
                            + '<a onClick="forceUpdate(\'' + row.appStore + '\',\'' + row.innerCode + '\',\'' + row.forceUpdate + '\')">已强制更新</a>&nbsp;&nbsp;&nbsp;&nbsp;'
                            + '<a href="javascript:void(0);" onClick="edit(\'' + row.appStore + '\',\'' + row.innerCode + '\')">编辑</a>&nbsp;&nbsp;&nbsp;&nbsp;'
                            + '<a href="javascript:void(0);" onClick="mode(\'' + row.appStore + '\',\'' + row.innerCode + '\')">上下架</a>&nbsp;&nbsp;&nbsp;&nbsp;'
                            + '<a href="javascript:void(0);" onClick="del(\'' + row.appStore + '\',\'' + row.innerCode + '\')">删除</a>';
                    } else if (row.tipUser = 'True' && row.forceUpdate == 'False') {
                        var btns = '<a  onClick="alertUpdate(\'' + row.appStore + '\',\'' + row.innerCode + '\')">已提示更新</a>&nbsp;&nbsp;&nbsp;&nbsp;'
                            + '<a onClick="forceUpdate(\'' + row.appStore + '\',\'' + row.innerCode + '\',\'' + row.forceUpdate + '\')">未强制更新</a>&nbsp;&nbsp;&nbsp;&nbsp;'
                            + '<a href="javascript:void(0);" onClick="edit(\'' + row.appStore + '\',\'' + row.innerCode + '\')">编辑</a>&nbsp;&nbsp;&nbsp;&nbsp;'
                            + '<a href="javascript:void(0);" onClick="mode(\'' + row.appStore + '\',\'' + row.innerCode + '\')">上下架</a>&nbsp;&nbsp;&nbsp;&nbsp;'
                            + '<a href="javascript:void(0);" onClick="del(\'' + row.appStore + '\',\'' + row.innerCode + '\')">删除</a>';
                    } else {
                        var btns = '<a  onClick="alertUpdate(\'' + row.appStore + '\',\'' + row.innerCode + '\')">已提示更新</a>&nbsp;&nbsp;&nbsp;&nbsp;'
                            + '<a onClick="forceUpdate(\'' + row.appStore + '\',\'' + row.innerCode + '\',\'' + row.forceUpdate + '\')">已强制更新</a>&nbsp;&nbsp;&nbsp;&nbsp;'
                            + '<a href="javascript:void(0);" onClick="edit(\'' + row.appStore + '\',\'' + row.innerCode + '\')">编辑</a>&nbsp;&nbsp;&nbsp;&nbsp;'
                            + '<a href="javascript:void(0);" onClick="mode(\'' + row.appStore + '\',\'' + row.innerCode + '\')">上下架</a>&nbsp;&nbsp;&nbsp;&nbsp;'
                            + '<a href="javascript:void(0);" onClick="del(\'' + row.appStore + '\',\'' + row.innerCode + '\')">删除</a>';
                    }
                    ;
                    return btns;
                }
            }],
            responseHandler: function (res) {
                return {
                    "total": res.total, //总页数
                    "rows": res.rows //数据
                };
            }
        });

        // 查询
        $("#search_btn").click(function () {
            table.bootstrapTable('refreshOptions', {pageNumber: 1});
        });


        // 新增
        $("#insert_btn").click(function () {
            window.parent.iw.dialog({
                size: 'lg',
                title: '发布版本',
                href: '/apps/insert',
                closing: function () {
                    table.bootstrapTable('refresh');
                }
            });
        });
        // 上传软件
        $("#upload_btn").click(function () {
            window.parent.iw.dialog({
                size: 'sm',
                title: '软件上传',
                href: '/apps/fileupload',
                closing: function () {
                    table.bootstrapTable('refresh');
                }
            });
        });
    });

    // 提示更新
    var alertUpdate = function (appstore, innercode) {
        if (confirm("是否修改更新状态？")) {
            $.post("/apps/update/", {
                appstore: appstore,
                innercode: innercode
            }, function (res) {
                alert(res);
                table.bootstrapTable('refresh');
            });
        }
    }
    // 上下架
    var mode = function (appstore, innercode) {
        if (confirm("是否修改状态？")) {
            $.post("/apps/mode/", {
                appstore: appstore,
                innercode: innercode
            }, function (res) {
                alert(res);
                table.bootstrapTable('refresh');
            });
        }
    }
    // 强制更新
    var forceUpdate = function (appstore, innercode, forceUpdate) {
        console.log(forceUpdate);
        if (forceUpdate == 'False') {
            if (confirm("是否强制更新？")) {
                $.post("/apps/forceUpdate", {
                    appstore: appstore,
                    innercode: innercode
                }, function (res) {
                    alert(res);
                    table.bootstrapTable('refresh');
                });
            }
        } else {
            if (confirm("是否取消强制更新？")) {
                $.post("/apps/forceUpdate", {
                    appstore: appstore,
                    innercode: innercode
                }, function (res) {
                    alert(res);
                    table.bootstrapTable('refresh');
                });

            }
        }
    }

    // 编辑
    var edit = function (appstore, innercode) {
        window.parent.iw.dialog({
            size: 'lg',
            title: '编辑版本',
            href: '/apps/edit/' + appstore + '/' + innercode,
            closing: function () {
                table.bootstrapTable('refresh');
            }
        });
    };
    //删除
    var del = function (appstore, innercode) {
        if (confirm("确定删除？")) {
            $.post("/apps/del/", {
                appstore: appstore,
                innercode: innercode
            }, function (res) {
                alert(res);
                table.bootstrapTable('refresh');
            });
        }
    }
</script>
</body>
</html>